<?php
class Memberscontroller{

	/**
	 * Registry object reference
	 */
	private $registry;

	/**
	 * Quotation model object reference
	 */
	private $model;

	/**
	 * Controller constructor - direct call to false when being embedded via another controller
	 * @param Registry $registry our registry
	 * @param bool $directCall - are we calling it directly via the framework (true), or via another controller (false)
	 */
	public function __construct( Registry $registry, $directCall )
	{
		$this->registry = $registry;

		$urlBits = $this->registry->getObject('url')->getURLBits();
		if( isset( $urlBits[1] ) )
		{
			switch( $urlBits[1] )
			{
				case 'list':
					{							
						$offset = 1;
							
						if (isset($_POST['page_num']))
						$offset = $_POST['page_num'];

						$this->listMembers( $offset );							
					}
					break;
				case 'search':
					{
						if (isset($_POST['searchword']))
						{
							$searchkey = $_POST['searchword'];
							$this->searchMembers($searchkey);
						}
						else
						$this->listMembers(1);
					}
					break;
				default:
					$this->listMembers(1);
					break;
			}

		}
		else
		{
			$this->listMembers( 1 );
		}

	}

	private function listMembers( $page )
	{
		$profileid = $this->registry->getObject('authenticate')->getUser()->getCurrentProfile()->getProfileID();
		$userid = $this->registry->getObject('authenticate')->getUser()->getUserID();

		$refresh = -1;
		if (isset($_POST['refresh']))
		$refresh = $_POST['refresh'];

		if ($refresh == 0)
		$this->registry->getObject('template')->buildFromTemplates('members/members_header.tpl.php', 'members/list_members.tpl.php', 'members/members_footer.tpl.php');
		else if ($refresh == 1)
		$this->registry->getObject('template')->buildFromTemplates('members/list_members.tpl.php', 'members/members_footer.tpl.php');
		else
		$this->registry->getObject('template')->buildFromTemplates('main/main.tpl.php', 'main/main_menu.tpl.php','main/main_content.tpl.php','members/members_header.tpl.php',
			'members/list_members.tpl.php', 'members/members_footer.tpl.php', 'main/footer.tpl.php');


		require_once( FRAMEWORK_PATH . 'models/members.php');
		$members = new Members( $this->registry );
		$loadLimit = 5;
		$loadedRows = 0;
		
		if ($page <= 0)
		 $page = 1;

		$offset = ($page - 1)*($members->limitPerPage);
			
		$cache = $members->listMembers( $offset, $profileid, $userid, $loadLimit );

		while ( ($loadedRows < 5 && $members->loadedRows > 0))
		{
			$this->registry->getObject('template')->getPage()->addPPTag('row_id', $loadedRows);
			$this->registry->getObject('template')->addTemplateBit('member_row'.$loadedRows, 'members/list_member_row.tpl.php');
			$this->registry->getObject('template')->getPage()->addPPTag( 'members'.$loadedRows, array( 'DATA', $cache ) );
			$this->registry->getObject('template')->parseOutput();
				
			++$loadedRows;
			$next = "{member_row{row_id}}";
			$this->registry->getObject('template')->getPage()->addPPTag('next_row', $next);
			$this->registry->getObject('template')->parseOutput();
				

			$offset+=$members->loadedRows;
				
			$cache = $members->listMembers( $offset, $profileid, $userid, $loadLimit );
				
		}

		//if ($refresh != 1)
		//{
		$pagenums = array();

		for($pagenum = 0; $pagenum < $members->totalPages; ++$pagenum)
		{
			$number['page_num'] = $pagenum + 1;
			$pagenums[] = $number;
		}

		$pagecache = $this->registry->getObject('db')->cacheData( $pagenums );
		$this->registry->getObject('template')->getPage()->addTag('pagenumbers', array('DATA', $pagecache));
		//}

		$this->registry->getObject('template')->getPage()->addTag('member_row{row_id}', '');

		$this->registry->getObject('template')->getPage()->addTag( 'page_number', $page );
		$this->registry->getObject('template')->getPage()->addTag( 'num_pages', $members->totalPages);

		unset ($_POST['refresh']);

		//$this->formRelationships();

	}

	private function listMembersAlpha( $alpha='A', $offset=0 )
	{
		require_once( FRAMEWORK_PATH . 'models/members.php');
		$members = new Members( $this->registry );
		$pagination = $members->listMembersByLetter( $alpha, $offset );
		if( $pagination->getNumRowsPage() == 0 )
		{
			$this->registry->getObject('template')->buildFromTemplates('header.tpl.php', 'members/invalid.tpl.php', 'footer.tpl.php');
		}
		else
		{
			$this->registry->getObject('template')->buildFromTemplates('header.tpl.php', 'members/list.tpl.php', 'footer.tpl.php');
			$this->registry->getObject('template')->getPage()->addTag( 'members', array( 'SQL', $pagination->getCache() ) );
			$this->registry->getObject('template')->getPage()->addTag( 'letter', " - Letter: " . $alpha );

			$this->registry->getObject('template')->getPage()->addTag( 'page_number', $pagination->getCurrentPage() );
			$this->registry->getObject('template')->getPage()->addTag( 'num_pages', $pagination->getNumPages() );
			if( $pagination->isFirst() )
			{
				$this->registry->getObject('template')->getPage()->addTag( 'first', '');
				$this->registry->getObject('template')->getPage()->addTag( 'previous', '' );
			}
			else
			{
				$this->registry->getObject('template')->getPage()->addTag( 'first', "<a href='members/alpha/".$alpha."/'>First page</a>" );
				$this->registry->getObject('template')->getPage()->addTag( 'previous', "<a href='members/alpha/".$alpha."/" . ( $pagination->getCurrentPage() - 2 ) . "'>Previous page</a>" );
			}
			if( $pagination->isLast() )
			{
				$this->registry->getObject('template')->getPage()->addTag( 'next', '' );
				$this->registry->getObject('template')->getPage()->addTag( 'last', '' );
			}
			else
			{
				$this->registry->getObject('template')->getPage()->addTag( 'first', "<a href='members/alpha/".$alpha."/" . $pagination->getCurrentPage() . "'>Next page</a>" );
				$this->registry->getObject('template')->getPage()->addTag( 'previous', "<a href='members/alpha/".$alpha."/" . ( $pagination->getNumPages() - 1 ) . "'>Last page</a>" );
			}
		}
	}

	private function searchMembers( $name )
	{
		require_once( FRAMEWORK_PATH . 'models/members.php');
		$members = new Members( $this->registry );

		$fullsearch = 0;
		if (isset($_POST['fullsearch']))
		$fullsearch = $_POST['fullsearch'];
			
		unset ($_POST['fullsearch']);

		$this->registry->getObject('template')->buildFromTemplates('members/member_search_result.tpl.php');

		$profileid = $this->registry->getObject('authenticate')->getUser()->getCurrentProfile()->getProfileID();
		$userid = $this->registry->getObject('authenticate')->getUser()->getUserID();

		$cache = $members->search($name, $profileid, $userid, $fullsearch);

		$this->registry->getObject('template')->getPage()->addTag( 'members', array( 'DATA', $cache ) );

		$this->registry->getObject('template')->parseOutput();


	}

	private function formRelationships()
	{
		if( $this->registry->getObject('authenticate')->isLoggedIn() == true )
		{
			require_once( FRAMEWORK_PATH . 'models/relationships.php');
			$relationships = new Relationships( $this->registry );
			$types = $relationships->getTypes( true );
			$this->registry->getObject('template')->addTemplateBit( 'form_relationship', 'members/form_relationship.tpl.php');
			$this->registry->getObject('template')->getPage()->addPPTag( 'relationship_types', array( 'SQL', $types ) );
		}
		else
		{
			$this->registry->getObject('template')->getPage()->addTag( 'form_relationship', '<!-- relationship types dropdown -->' );
		}
	}
}

?>